
#include <glog/logging.h>
#include <thread>

void hello() { LOG(INFO) << "hello concurrency and parallelism world"; }

void do_something(int i) { LOG(INFO) << "do_something: " << i; }

struct Func {
    int& i;
    Func(int& i_)
        : i(i_)
    {
    }
    void operator()()
    {
        for (unsigned j = 0; j < 10; ++j) {
            do_something(i++);
        }
    }
};

void func(int& i)
{
    for (unsigned j = 0; j < 10; ++j) {
        do_something(i++);
    }
}

void oops()
{
    int some_local_state = 0;
    Func my_func(some_local_state);
    std::thread my_thread(my_func);
    my_thread.join();
    LOG(INFO) << "after: " << some_local_state;
}

void oops2()
{
    int some_local_state = 0;
    std::thread my_thread(func, std::ref(some_local_state));
    my_thread.join();
    LOG(INFO) << "after: " << some_local_state;
}

int main(int argc, char* argv[])
{
    google::SetStderrLogging(google::LogSeverity(FLAGS_minloglevel));
    google::InitGoogleLogging(argv[0]);

    LOG(INFO) << "hello world";
    std::thread t(hello);
    t.join();

    oops();
    oops2();
    return 0;
}